home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / sheriffa / sheriff.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-27  |  7.4 KB  |  301 lines

  1. // This is a part of the Sheriff System Development Kit.
  2. // Copyright (C) 1997-1998 Acudata Limted.
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Sheriff System Development Kit and related
  7. // electronic documentation provided with the SDK.
  8.  
  9. #include "stdafx.h"
  10. #include <stdlib.h>
  11. #include <stdio.h>
  12. #include <time.h>
  13.  
  14. #include "Sheriff.h"
  15.  
  16. CSheriff::CSheriff(LPCTSTR lpszProductID,LPCTSTR lpszUserName)
  17. {
  18.     m_strProductID=lpszProductID;
  19.     m_strUserName=lpszUserName;
  20.  
  21.     //Zero secrets
  22.     for(int i=0;i<4;i++)
  23.         memset(m_arySecrets[i].Secret,0,sizeof(SLS_SECRET));
  24.     m_bSecretsSet=FALSE;
  25.     //
  26.     m_hLicence=0L;
  27.     m_lLastError=SLS_SUCCESS;
  28. }
  29.  
  30. CSheriff::~CSheriff()
  31. {
  32. }
  33.  
  34. void CSheriff::SetSecrets(SLS_SECRET *pSecrets,int nSizeSecrets)
  35. {
  36.     for(int i=0;i<min(nSizeSecrets,4);i++)
  37.         memcpy(m_arySecrets[i].Secret,pSecrets[i].Secret,sizeof(SLS_SECRET));
  38.     m_bSecretsSet=TRUE;
  39. }
  40.  
  41. void CSheriff::GetLastErrorMessage(CString &strError)
  42. {
  43.     char szErrorMessage[256];
  44.     SLS_GetErrorMessage(m_lLastError,szErrorMessage);
  45.     strError=szErrorMessage;
  46. }
  47.  
  48. BOOL CSheriff::QueryLicenceInfo(SLS_LICENCE_INFO &licInfo)
  49. {
  50.     m_lLastError=SLS_QueryLicenceInfo(m_strProductID,&licInfo);
  51.     return SUCCEEDED(m_lLastError);
  52. }
  53.  
  54. BOOL CSheriff::GetReference(CString &strReference)
  55. {
  56.     char szReference[128];
  57.     m_lLastError=SLS_GetReference(m_strProductID,szReference);
  58.     strReference=szReference;
  59.     return SUCCEEDED(m_lLastError);
  60. }
  61.  
  62. BOOL CSheriff::Register(LPCTSTR lpszProductName,LPCTSTR lpszLicencePath)
  63. {
  64.     m_lLastError=SLS_Register(m_strProductID,lpszProductName,lpszLicencePath);
  65.     return SUCCEEDED(m_lLastError);
  66. }
  67.  
  68. BOOL CSheriff::SetLicence(LPCSTR pszReference,LPCTSTR lpszLicenceKey)
  69. {
  70.     m_lLastError=SLS_SetLicence(m_strProductID,pszReference,lpszLicenceKey);
  71.     return SUCCEEDED(m_lLastError);
  72. }
  73.  
  74. BOOL CSheriff::License(SLS_LICENCE Licence)
  75. {
  76.     if(!m_bSecretsSet)
  77.     {
  78.         //Secrets Undefined
  79.         m_lLastError=SLS_E_BAD_SECRET;
  80.         return FALSE;
  81.     }
  82.  
  83.     //Create Challenge
  84.     SLS_CHALLENGE Challenge;
  85.     m_lLastError=SLS_CreateChallenge(m_arySecrets,4,(const BYTE *)&Licence,sizeof(SLS_LICENCE),&Challenge);
  86.     if(!SUCCEEDED(m_lLastError))
  87.         return FALSE;
  88.     m_lLastError=SLS_License(m_strProductID,&Licence,&Challenge);
  89.     
  90.     return SUCCEEDED(m_lLastError);
  91. }
  92.  
  93. BOOL CSheriff::Request(SLS_REQUEST Request,SLS_PERMIT &Permit)
  94. {
  95.     memset(&Permit,0,sizeof(Permit));
  96.     
  97.     SLS_CHALLENGE Challenge;
  98.     if(m_bSecretsSet)
  99.     {
  100.         //Create Challenge
  101.         m_lLastError=SLS_CreateChallenge(m_arySecrets,4,(const BYTE *)&Request,sizeof(SLS_REQUEST),&Challenge);
  102.         if(!SUCCEEDED(m_lLastError))
  103.             return FALSE;
  104.         m_lLastError=SLS_Request(m_strProductID,m_strUserName,&Request,&Permit,&m_hLicence,&Challenge);
  105.         if(SUCCEEDED(m_lLastError))
  106.         {
  107.             //Verify Challenge
  108.             m_lLastError=SLS_VerifyChallenge(m_arySecrets,4,(const BYTE *)&Permit,sizeof(SLS_PERMIT),&Challenge);
  109.         }
  110.     }
  111.     else
  112.     {
  113.         //No Challenge
  114.         Challenge.Protocol=SLS_NO_PROTOCOL;
  115.         m_lLastError=SLS_Request(m_strProductID,m_strUserName,&Request,&Permit,&m_hLicence,&Challenge);
  116.     }    
  117.     
  118.     return SUCCEEDED(m_lLastError);
  119. }
  120.  
  121. BOOL CSheriff::Update(SLS_UPDATE Update,SLS_PERMIT &Permit)
  122. {
  123.     memset(&Permit,0,sizeof(Permit));
  124.     
  125.     SLS_CHALLENGE Challenge;
  126.     if(m_bSecretsSet)
  127.     {
  128.         //Create Challenge
  129.         m_lLastError=SLS_CreateChallenge(m_arySecrets,4,(const BYTE *)&Update,sizeof(SLS_UPDATE),&Challenge);
  130.         if(!SUCCEEDED(m_lLastError))
  131.             return FALSE;
  132.         m_lLastError=SLS_Update(m_strProductID,m_hLicence,&Update,&Permit,&Challenge);    
  133.         if(SUCCEEDED(m_lLastError))
  134.         {
  135.             //Verify Challenge
  136.             m_lLastError=SLS_VerifyChallenge(m_arySecrets,4,(const BYTE *)&Permit,sizeof(SLS_PERMIT),&Challenge);
  137.         }
  138.     }
  139.     else
  140.     {
  141.         //No Challenge
  142.         Challenge.Protocol=SLS_NO_PROTOCOL;
  143.         m_lLastError=SLS_Update(m_strProductID,m_hLicence,&Update,&Permit,&Challenge);    
  144.     }
  145.  
  146.     return SUCCEEDED(m_lLastError);
  147. }
  148.  
  149. BOOL CSheriff::Release(SLS_RELEASE Release)
  150. {
  151.     SLS_CHALLENGE Challenge;
  152.     if(m_bSecretsSet)
  153.     {
  154.         //Create Challenge
  155.         SLS_CreateChallenge(m_arySecrets,4,(const BYTE *)&Release,sizeof(SLS_RELEASE),&Challenge);
  156.     }
  157.     else
  158.     {
  159.         //No Challenge
  160.         Challenge.Protocol=SLS_NO_PROTOCOL;
  161.     }
  162.  
  163.     m_lLastError=SLS_Release(m_strProductID,m_hLicence,&Release,&Challenge);
  164.     
  165.     return SUCCEEDED(m_lLastError);
  166. }
  167.  
  168. BOOL CSheriff::IsProductInstalled()
  169. {
  170.     m_lLastError=SLS_IsProductInstalled(m_strProductID);
  171.     return SUCCEEDED(m_lLastError);
  172. }
  173.  
  174. BOOL CSheriff::IsProductRegistered()
  175. {
  176.     m_lLastError=SLS_IsProductRegistered(m_strProductID);
  177.     return SUCCEEDED(m_lLastError);
  178. }
  179.  
  180. BOOL CSheriff::IsProductLicensed()
  181. {
  182.     m_lLastError=SLS_IsProductLicensed(m_strProductID);
  183.     return SUCCEEDED(m_lLastError);
  184. }
  185.  
  186. BOOL CSheriff::IsLicenceDefined()
  187. {
  188.     SLS_LICENCE_INFO licInfo;
  189.  
  190.     m_lLastError=SLS_QueryLicenceInfo(m_strProductID,&licInfo);
  191.     if(!SUCCEEDED(m_lLastError))
  192.         return FALSE;
  193.     if(licInfo.State==SLS_STATE_UNDEFINED || licInfo.State==SLS_STATE_BAD)
  194.         return FALSE;
  195.     return TRUE;
  196. }
  197.  
  198. BOOL CSheriff::IsLicenceValid()
  199. {
  200.     SLS_LICENCE_INFO licInfo;
  201.  
  202.     m_lLastError=SLS_QueryLicenceInfo(m_strProductID,&licInfo);
  203.     if(!SUCCEEDED(m_lLastError))
  204.         return FALSE;
  205.     return licInfo.State==SLS_STATE_OK;
  206. }
  207.  
  208. BOOL CSheriff::IsLicenceExportable()
  209. {
  210.     SLS_LICENCE_INFO licInfo;
  211.  
  212.     m_lLastError=SLS_QueryLicenceInfo(m_strProductID,&licInfo);
  213.     if(!SUCCEEDED(m_lLastError))
  214.         return FALSE;
  215.     return !(licInfo.Type & SLS_TYPE_UNEXPORTABLE);
  216. }
  217.  
  218. BOOL CSheriff::IsStandaloneKey()
  219. {
  220.     SLS_LICENCE_INFO licInfo;
  221.  
  222.     m_lLastError=SLS_QueryLicenceInfo(m_strProductID,&licInfo);
  223.     if(!SUCCEEDED(m_lLastError))
  224.         return FALSE;
  225.     return (licInfo.Type & SLS_TYPE_STANDALONE);
  226. }
  227.  
  228. BOOL CSheriff::IsLifetimeKey()
  229. {
  230.     SLS_LICENCE_INFO licInfo;
  231.  
  232.     m_lLastError=SLS_QueryLicenceInfo(m_strProductID,&licInfo);
  233.     if(!SUCCEEDED(m_lLastError))
  234.         return FALSE;
  235.     return (licInfo.Type & SLS_TYPE_LIFETIME_KEY);
  236. }
  237.  
  238. BOOL CSheriff::IsLifetimeRef()
  239. {
  240.     SLS_LICENCE_INFO licInfo;
  241.  
  242.     m_lLastError=SLS_QueryLicenceInfo(m_strProductID,&licInfo);
  243.     if(!SUCCEEDED(m_lLastError))
  244.         return FALSE;
  245.     return (licInfo.Type & SLS_TYPE_LIFETIME_REF);
  246. }
  247.  
  248. BOOL CSheriff::SetOptions(SLS_OPTIONS Options)
  249. {
  250.     if(!m_bSecretsSet)
  251.     {
  252.         //Secrets Undefined
  253.         m_lLastError=SLS_E_BAD_SECRET;
  254.         return FALSE;
  255.     }
  256.  
  257.     //Create Challenge
  258.     SLS_CHALLENGE Challenge;
  259.     m_lLastError=SLS_CreateChallenge(m_arySecrets,4,(const BYTE *)&Options,sizeof(SLS_OPTIONS),&Challenge);
  260.     if(!SUCCEEDED(m_lLastError))
  261.         return FALSE;
  262.     m_lLastError=SLS_SetOptions(m_strProductID,m_hLicence,&Options,&Challenge);
  263.     
  264.     return SUCCEEDED(m_lLastError);
  265. }
  266.  
  267. BOOL CSheriff::Export(SLS_LICENCE Licence,LPCTSTR lpszExportRef,CString &strExportKey)
  268. {
  269.     char szExportKey[128];
  270.     m_lLastError=SLS_ExportLicence(m_strProductID,lpszExportRef,&Licence,szExportKey);
  271.     strExportKey=szExportKey;
  272.     return SUCCEEDED(m_lLastError);
  273. }
  274.  
  275. BOOL CSheriff::Import(LPCSTR pszReference,LPCTSTR lpszLicenceKey)
  276. {
  277.     m_lLastError=SLS_ImportLicence(m_strProductID,pszReference,lpszLicenceKey);
  278.     return SUCCEEDED(m_lLastError);
  279. }
  280.  
  281. DWORD CSheriff::GetUserCount()
  282. {
  283.     DWORD dwUserCount;
  284.     SLS_GetUserCount(m_strProductID,&dwUserCount);
  285.     return dwUserCount;
  286. }
  287.  
  288. BOOL  CSheriff::QueryUserInfo(DWORD dwUserIndex,SLS_USER_INFO &userInfo)
  289. {
  290.     //dwUserIndex is 1 based
  291.     DWORD dwUserCount=GetUserCount();
  292.     if(dwUserIndex > dwUserCount)
  293.     {
  294.         m_lLastError = SLS_E_BAD_USER_INDEX;
  295.         return FALSE;
  296.     }
  297.     m_lLastError=SLS_QueryUserInfo(m_strProductID,dwUserIndex,&userInfo);
  298.     return SUCCEEDED(m_lLastError);
  299. }
  300.  
  301.